package com.feini.ktv.svc.controller.om;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feini.ktv.svc.entity.om.OmBoxH;
import com.feini.ktv.svc.entity.om.OmOfferH;
import com.feini.ktv.svc.service.om.IOmBoxHService;
import com.feini.ktv.svc.service.om.IOmOfferHService;
import com.podara.skeleton.controller.SkeletonController;
import com.podara.skeleton.log.annotation.LogLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiangzr
 * @since 2021-04-16
 */
@LogLogger
@RestController
public class OmOfferHController implements SkeletonController {

    @Autowired
    private IOmOfferHService offerHService;
    @Autowired
    private IOmBoxHService boxHService;

    @GetMapping("/queryOldShoppingCartList")
    public List<Map<String, Object>> queryShoppingCartList() {
        QueryWrapper<OmOfferH> offerQueryWrapper = new QueryWrapper<OmOfferH>();
        offerQueryWrapper.eq("order_status", "0");
        offerQueryWrapper.eq("data_status", "0");
        List<Map<String, Object>> offerList = this.offerHService.listMaps(offerQueryWrapper);
        if (null != offerList && offerList.size() > 0) {
            String orderId = "";
            String boxId = "";
            String boxName = "";
            for (Map<String, Object> offerDc : offerList) {
                String newOrderId = offerDc.get("order_id").toString();
                if ("".equals(orderId) || !Objects.equals(orderId, newOrderId)) {
                    orderId = newOrderId;
                    QueryWrapper<OmBoxH> boxQueryWrapper = new QueryWrapper<OmBoxH>();
                    boxQueryWrapper.eq("order_id", "" + orderId);
                    List<OmBoxH> boxList = this.boxHService.list(boxQueryWrapper);
                    if (null != boxList && boxList.size() > 0) {
                        OmBoxH boxMap = boxList.get(0);
                        boxId = boxMap.getBoxId() + "";
                        boxName = boxMap.getBoxName();
                        offerDc.put("boxId", boxId);
                        offerDc.put("boxName", boxName);
                    }
                } else {
                    offerDc.put("boxId", boxId);
                    offerDc.put("boxName", boxName);
                }
            }

        }

        return offerList;
    }

    @GetMapping("/queryOldShoppingCartByCond/{boxId}")
    public List<Map<String, Object>> queryShoppingCartByCond(@PathVariable("boxId") String boxId) {
        QueryWrapper<OmBoxH> boxQueryWrapper = new QueryWrapper<OmBoxH>();
        boxQueryWrapper.eq("box_id", boxId);
        List<OmBoxH> boxList = this.boxHService.list(boxQueryWrapper);

        List<Map<String, Object>> offerList = new ArrayList<>();
        if (null != boxList && boxList.size() > 0) {
            OmBoxH boxMap = boxList.get(0);
            QueryWrapper<OmOfferH> offerQueryWrapper = new QueryWrapper<OmOfferH>();
            offerQueryWrapper.eq("order_status", "0");
            offerQueryWrapper.eq("data_status", "0");
            offerQueryWrapper.eq("order_id", boxMap.getOrderId());
            offerList = this.offerHService.listMaps(offerQueryWrapper);
            for (Map<String, Object> offerDc : offerList) {
                offerDc.put("boxId", boxId);
                offerDc.put("boxName", boxMap.getBoxName());
            }
        }
        return offerList;
    }

}

